import React from 'react';
import {Select} from 'antd';
import {withRouter} from 'react-router-dom'
import { injectIntl } from 'react-intl';

const Option = Select.Option;


@withRouter
class YearSelect  extends React.Component {
    constructor(props) {		
        super(props);        
        this.state={
        }
        this.getYearList = this.getYearList.bind(this);
        this.getYear = this.getYear.bind(this);
    }

    componentWillMount(){
       let list = this.getYearList();
       let value = this.getYear();
       console.log(value)
       this.setState({
           content:[
                ...list
           ],
           defaultYear:[
                value
           ]
       })
    }

    getYear(){
        let myDate  = new Date();
        let year = myDate.getFullYear();
        return  year;
    }

    //根据当前的年,算出后面15年的时间
    getYearList=()=>{
        let yearParam = [];
        let yearArr = new Array();
        let myDate  = new Date();
        let year = myDate.getFullYear(); //获取完整的年份(4位,1970-????)
        for(let  i=0;i<15;i++){
            let jsonObj = {};
            jsonObj['value']=year -i;
            jsonObj['name']=year -i+this.props.intl.messages.common.year;
            yearArr.push(jsonObj);
        }
        return yearArr;
    }

    getChargeYear=(value)=>{
        this.setState({
            defaultYear:value
        })
        this.props.changeYear(value);
    }

    render(){
        return(
                <Select value={this.state.defaultYear} style={{ width: 120,marginRight:'20px' }}  onChange={this.getChargeYear}>
                    {
                        this.state.content.map((child) => {
                            return (<Option key={child.value} value={child.value}>{child.name}</Option>)
                        })
                    }
                </Select>
        )
    }
}    
export default injectIntl(YearSelect);